package com.cqs.leetcode.math;

import java.util.Arrays;

/**
 * lixw
 * 2023/10/29
 */
public class Solution274 {
    public int hIndex(int[] citations) {
        //[2]
        Arrays.sort(citations);
        int l = 0, r = citations.length - 1;
        while (l <= r) {
            int mid = (l + r) / 2;
            //相等
            if (citations[mid] == citations.length - mid) {
                return citations[mid];
            }
            //引用次数 < 文章数。
            if (citations[mid] < citations.length - mid) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return citations.length - l;
    }

    public int hIndex1(int[] citations) {
        Arrays.sort(citations);
        int result = 0;
        int i = citations.length - 1;
        while (citations[i] > result) {
            --i;
            ++result;
        }
        return result;
    }
}
